package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.exceptions.ThrowPageException;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.AgMenus;
import com.sxbbc.common.core.entity.jz.AgRoles;
import com.sxbbc.common.core.mapper.jz.AgMenusMapper;
import com.sxbbc.common.core.mapper.jz.AgRolesMapper;
import com.sxbbc.common.core.service.IAgRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代理角色表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-10
 */
@Service
public class AgRolesServiceImpl extends ServiceImpl<AgRolesMapper, AgRoles> implements IAgRolesService {

    @Autowired
    private AgMenusMapper agMenusMapper;

    @Override
    public List<AgMenus> queryMenusByRoleId(Integer id) {
        String menuIds = "";
        if (id != null) {
            AgRoles role = super.getById(id);
            if (role == null) {
                throw new ThrowPageException("未找到角色信息");
            }
            menuIds = "," + role.getMenuIds() + ",";
        }
        QueryWrapper<AgMenus> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StaticUtils.STATUS_YES);
        wrapper.orderByAsc("id,type");
        List<AgMenus> list = agMenusMapper.selectList(wrapper);
        for (AgMenus l : list) {
            if (StringUtils.isNotBlank(menuIds)) {
                String m = "," + l.getId() + ",";
                if (menuIds.indexOf(m) > -1) {
                    l.setCheck("checked");
                }
            }
        }
        return list;
    }
}
